using System;
using UnityEngine;
using UnityEngine.InputSystem;

public class GrowBlock : MonoBehaviour
{
    public enum GrowthStage
    {
        // 荒芜的，未开垦的阶段
        barren,
        // 已开垦的阶段
        ploughed,
        // 已种植的阶段
        planted,
        // 生长阶段 1
        growing1,
        // 生长阶段 2
        growing2,
        // 成熟的阶段
        ripe

    }

    public GrowthStage currentStage;
    public SpriteRenderer theSR;
    public Sprite soilTilled, soilWatered;
    public SpriteRenderer cropSR;
    public Sprite cropPlanted, cropGrowing1, cropGrowing2, cropRipe;
    public bool isWatered;
    public bool preventUse;

    private void Update()
    {
        // if (Keyboard.current.eKey.wasPressedThisFrame)
        // {
        //     AdvanceStage();
        //     
        //     SetSoilSprite();
        // }
        
        #if UNITY_EDITOR
        if (Keyboard.current.nKey.wasPressedThisFrame)
        {
            AdvanceCrop();
        }
        #endif
    }

    public void AdvanceStage()
    {
        currentStage += 1;
        
        if ((int)currentStage >= 6)
        {
            currentStage = GrowthStage.barren;
        }
    }

    public void SetSoilSprite()
    {
        if (currentStage == GrowthStage.barren)
        {
            theSR.sprite = null;
        }
        else
        {
            if (isWatered)
            {
                theSR.sprite = soilWatered;
            }
            else
            {
                theSR.sprite = soilTilled;
            }
        }
    }

    public void PloughSoil()
    {
        if (currentStage == GrowthStage.barren && !preventUse)
        {
            currentStage = GrowthStage.ploughed;
            
            SetSoilSprite();
        }
    }

    public void WaterSoil()
    {
        if (currentStage == GrowthStage.barren || preventUse)
        {
            return;
        }
        
        isWatered = true;
        SetSoilSprite();
    }

    public void PlantCrop()
    {
        if (currentStage == GrowthStage.ploughed && isWatered && !preventUse)
        {
            currentStage = GrowthStage.planted;
            UpdateCropSprite();
        }
    }

    void UpdateCropSprite()
    {
        switch (currentStage)
        {
            case GrowthStage.planted:
                cropSR.sprite = cropPlanted;
                break;
            case GrowthStage.growing1:
                cropSR.sprite = cropGrowing1;
                break;
            case GrowthStage.growing2:
                cropSR.sprite = cropGrowing2;
                break;
            case GrowthStage.ripe:
                cropSR.sprite = cropRipe;
                break;
            default:
                throw new ArgumentOutOfRangeException();
        }
    }
    
    public void AdvanceCrop()
    {
        if (isWatered && !preventUse)
        {
            if (currentStage == GrowthStage.planted || currentStage == GrowthStage.growing1 || currentStage == GrowthStage.growing2)
            {
                currentStage++;
                isWatered = false;
                SetSoilSprite();
                UpdateCropSprite();
            }
        }
    }

    public void HarvestCrop()
    {
        if (currentStage == GrowthStage.ripe && !preventUse)
        {
            currentStage = GrowthStage.ploughed;
            SetSoilSprite();
            cropSR.sprite = null;
        }
    }
}
